<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Facades\Validator;
use Carbon\Carbon;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        Schema::defaultStringLength(191);
        Validator::extend("is_mobile_phone",function($attribute, $value, $parameters){
            return preg_match("/^((\+?[0-9]{1,4})|(\(\+86\)))?(13[0-9]|14[57]|15[012356789]|17[03678]|18[0-9])\d{8}$/",$value) === 1;
        });
        Validator::extend("is_past",function($attr,$v,$params,$validator){
            $validator->addReplacer('is_past', function($message, $attribute, $rule, $parameters) use($params){
                $now = Carbon::now();
                $parameters[1] = $now->subYears($params[0]+0)->format("Y-m-d");
                return str_replace([':years',':date'], $parameters, $message);
            });
            if(strtotime($v) === false) return false;
            $date = new Carbon($v);

            $now = Carbon::now();
            if($date->diffInYears($now)>=$params[0]){
                return true;
            }
            return false;
        });
    }

    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }
}
